﻿using Adp;
using Adp.Common;
using Adp.Core.Attribute;
using Microsoft.AspNetCore.Mvc;
using Yqd.Core;
using Yqd.Enm;
using Yqd.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yqd.Controllers.Filter;
using System.Threading;

namespace Yqd.Controllers
{
    public class QuotaMsgController : BaseController
    {
        YqdContext ctx;

        public QuotaMsgController(YqdContext ctx)
        {
            this.ctx = ctx;
        }

        //[Master]
        public ActionResult GetList()
        {
            var start = Form.GetInt("start");
            var limit = Form.GetInt("limit");
            var key = Form.GetString("key");
            var Year = Form.GetString("Year", "2021");
            var where = " Year = {0} ";
            if (key.Trim() != "")
            {
                where += " and lower(ltrim(rtrim([TaskName]))) like {1}";
            }
            where += " and (TaskID  in ( Select TaskID From Yqd_QuotaMsg Where IsShow=1))";
            //if (unbind)
            //{
            //    where += " and (OrganizationCode is null or OrganizationCode='')";
            //}
            var (totalcount, data) = ctx.Database.SqlPager<Yqd_Task>(start, limit, "*", where, "[CreateTime] Desc", Year, "%" + key.ToLower().Trim() + "%");
            var Qm = ctx.Yqd_QuotaMsg.ToList();
            return this.ToJson(new
            {
                success = true,
                totalcount,
                data = data.Select(q => new
                {
                    q.TaskName,
                    TaskID = q.TaskID.ToString(),
                    q.LeaderUnitName,
                    UpCount = Qm.Count(it => it.TaskID == q.TaskID && it.IsShow)
                })
            });

        }

        public ActionResult GetTaskN_O()
        {
            var TaskID = Form.GetLong("TaskID");
            var qml = ctx.Yqd_QuotaMsg.Where(it => it.TaskID == TaskID && it.IsShow).ToList();
            var qmcode = qml.Select(it => it.QuotaCode);

            var QuotaList = ctx.Yqd_Quota.Where(it => it.TaskID == TaskID && it.IsShow).ToList();
            var Milestone = ctx.Yqd_Milestone.Where(it => it.TaskID == TaskID && it.IsShow).ToList();

            var QuotaList_N = ctx.Yqd_QuotaNew.Where(it => it.TaskID == TaskID && it.IsShow && qmcode.Contains(it.QuotaCode)).ToList();
            var Milestone_N = ctx.Yqd_MilestoneNew.Where(it => it.TaskID == TaskID && it.IsShow && qmcode.Contains(it.TaskQuotaCode)).ToList();


            var arr = new ArrayList();
            var Narr = new ArrayList();
            foreach (var Q in QuotaList)
            {
                var qm = Milestone.Where(it => it.TaskQuotaCode == Q.QuotaCode).OrderBy(it => it.MilestoneTime).ToList();
                arr.Add(new
                {
                    Q.ds,
                    Q.QuotaName,
                    Q.QuotaCode,
                    Q.ValueUnit,
                    Q.QuotaType,
                    Q.ValueType,
                    Q.StartValue,
                    Q.CurrentValue,
                    Q.TargetValue,
                    Q.Calucation,
                    MList = qm.Select(it => new
                    {
                        it.MilestoneTime,
                        it.TargetValue,
                        MT = it.MilestoneTime.ToString("M月")
                    }),
                    MsgList = qml.Where(it => it.QuotaCode == Q.QuotaCode && it.IsShow).Select(it => new
                    {
                        it.ds,
                        it.Msg
                    })
                });
            }

            foreach (var Q in QuotaList_N)
            {
                var qm = Milestone_N.Where(it => it.TaskQuotaCode == Q.QuotaCode).OrderBy(it => it.MilestoneTime).ToList();
                var oqm = Milestone.Where(it => it.TaskQuotaCode == Q.QuotaCode).OrderBy(it => it.MilestoneTime).ToList();
                //if (qm.Count() ==0)
                //{
                //    qm = Milestone.Where(it => it.TaskQuotaCode == Q.QuotaCode).OrderBy(it => it.MilestoneTime).ToList();
                //}
                Narr.Add(new
                {
                    Q.ds,
                    Q.QuotaName,
                    Q.QuotaCode,
                    Q.ValueUnit,
                    Q.QuotaType,
                    Q.ValueType,
                    Q.StartValue,
                    Q.CurrentValue,
                    Q.TargetValue,
                    Q.Calucation,
                    MList = qm.Count() == 0 ? (oqm.Select(it => new
                    {
                        it.MilestoneTime,
                        it.TargetValue,
                        MT = it.MilestoneTime.ToString("M月")
                    })) : (qm.Select(it => new
                    {
                        it.MilestoneTime,
                        it.TargetValue,
                        MT = it.MilestoneTime.ToString("M月")
                    }))

                });
            }

            return this.ToJson(new
            {
                success = true,
                data_O = arr,
                data_N = Narr
            });

        }

        public ActionResult upNewsData()
        {
            var TaskIDs = Form.GetLongArray("TaskIDs");
            var QuotaList = ctx.Yqd_Quota.Where(it => TaskIDs.Contains(it.TaskID) && it.IsShow);
            var NQuotaList = ctx.Yqd_QuotaNew.Where(it => TaskIDs.Contains(it.TaskID) && it.IsShow);
            var MilestoneList = ctx.Yqd_Milestone.Where(it => TaskIDs.Contains(it.TaskID) && it.IsShow);
            var NMilestoneList = ctx.Yqd_MilestoneNew.Where(it => TaskIDs.Contains(it.TaskID) && it.IsShow);
            var qMsg = ctx.Yqd_QuotaMsg.Where(it => TaskIDs.Contains(it.TaskID));

            var AQL = new List<Yqd_Quota>();
            var AML = new List<Yqd_Milestone>();
            var Tasks = ctx.Yqd_Task.Where(it => TaskIDs.Contains(it.TaskID));
            foreach (var Q in QuotaList)
            {
                var isQMsg = qMsg.Any(it => it.QuotaCode == Q.QuotaCode && it.TField == "Yqd_Quota");

                if (isQMsg)
                {
                    var NQ = NQuotaList.FirstOrDefault(it => it.QuotaCode == Q.QuotaCode);
                    Q.IsShow = false;
                    if (NQ.QuotaID == Q.QuotaID)
                    {
                        Q.QuotaName = NQ.QuotaName;
                        Q.QuotaCode = NQ.QuotaCode;
                        Q.TaskID = NQ.TaskID;
                        Q.QuotaID = NQ.QuotaID;
                        Q.QuotaType = NQ.QuotaType;
                        Q.TargetValue = NQ.TargetValue;
                        Q.CurrentValue = NQ.CurrentValue;
                        Q.StartValue = NQ.StartValue;
                        Q.ValueType = NQ.ValueType;
                        Q.ValueUnit = NQ.ValueUnit;
                        Q.QuotaDesc = NQ.QuotaDesc;
                        Q.Calucation = NQ.Calucation;
                        Q.ds = NQ.ds;
                        Q.CreateTime = NQ.CreateTime;
                        Q.Tactic = NQ.Tactic;
                        Q.IsShow = NQ.IsShow;
                        Q.Creator = NQ.Creator;
                        Q.InsTime = NQ.InsTime;

                    }
                    else
                    {
                        AQL.Add(new Yqd_Quota
                        {
                            QuotaName = NQ.QuotaName,
                            QuotaCode = NQ.QuotaCode,
                            TaskID = NQ.TaskID,
                            QuotaID = NQ.QuotaID,
                            QuotaType = NQ.QuotaType,
                            TargetValue = NQ.TargetValue,
                            CurrentValue = NQ.CurrentValue,
                            StartValue = NQ.StartValue,
                            ValueType = NQ.ValueType,
                            ValueUnit = NQ.ValueUnit,
                            QuotaDesc = NQ.QuotaDesc,
                            Calucation = NQ.Calucation,
                            ds = NQ.ds,
                            CreateTime = NQ.CreateTime,
                            Tactic = NQ.Tactic,
                            IsShow = NQ.IsShow,
                            Creator = NQ.Creator,
                            InsTime = NQ.InsTime
                        });

                    }
                   
                    NQ.IsShow = false;
                    //ctx.Yqd_QuotaNew.Remove(NQ);
                }

                var isMMsg = qMsg.Any(it => it.QuotaCode == Q.QuotaCode && it.TField == "Yqd_Milestone");

                if (isMMsg)
                {
                    var NML = NMilestoneList.Where(it => it.TaskQuotaCode == Q.QuotaCode);
                    var ML = MilestoneList.Where(it => it.TaskQuotaCode == Q.QuotaCode);
                    foreach (var NM in NML)
                    {

                        AML.Add(new Yqd_Milestone
                        {
                            TaskID = NM.TaskID,
                            MilestoneID = NM.MilestoneID,
                            TaskQuotaCode = NM.TaskQuotaCode,
                            TargetValue = NM.TargetValue,
                            MilestoneTime = NM.MilestoneTime,
                            ds = NM.ds,
                            IsShow = NM.IsShow,
                            InsTime = NM.InsTime,
                            Tactic = NM.Tactic,
                            Creator = NM.Creator
                        });
                        NM.IsShow = false;
                        //ctx.Yqd_MilestoneNew.Remove(NM);
                    }

                    foreach (var M in ML)
                    {
                        M.IsShow = false;
                    }

                }

                var Task = Tasks.FirstOrDefault(it=>it.TaskID== Q.TaskID);
                Task.TaskStatus = (int)Enm.任务状态.未考核;

            }
            ctx.Yqd_Quota.AddRange(AQL);
            ctx.Yqd_Milestone.AddRange(AML);
          
            if (TaskIDs.Length > 0)
            {
                var sql = string.Format("update Yqd_QuotaMsg set IsShow=0 where TaskID in ({0})", string.Join(",", TaskIDs));
                ctx.Database.ExecuteSql(sql);
            }
            var r = ctx.SaveChanges();
            return this.ToJson(new
            {
                success = true
            });
        }
    }
}
